
//
//  AuthorityManager.m
//  DoctorYL
//
//  Created by Lisa on 16/12/1.
//  Copyright © 2016年 yuntai. All rights reserved.
//

#import "AuthorityManager.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
#import <CoreLocation/CoreLocation.h>

@interface AuthorityManager ()<UIAlertViewDelegate>


@end

@implementation AuthorityManager

static AuthorityManager *sharedInstance = nil;
#pragma mark Singleton Model
+ (AuthorityManager *)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[AuthorityManager alloc]init];
    });
    return sharedInstance;
}

/**
 *  判断是否有相机权限
 *
 *  @return
 */
- (BOOL)judgeCaramerStatus
{
    //相机权限
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus ==AVAuthorizationStatusRestricted ||//此应用程序没有被授权访问的照片数据。可能是家长控制权限
        authStatus ==AVAuthorizationStatusDenied)  //用户已经明确否认了这一照片数据的应用程序访问
    {
        UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"提示:"
                                                           message:@"尚未开启相机权限,您可以前往设置->隐私->相册授权应用访问相机权限"
                                                          delegate:self
                                                 cancelButtonTitle:@"取消"
                                                 otherButtonTitles:@"确定", nil];
        [alterView show];
        return NO;
    }else if (AVAuthorizationStatusNotDetermined == authStatus){
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                        
        }];
        
        return NO;
    }
    return YES;
}
- (void)judgeCaramerStatus:(void(^)(BOOL status))statusblock{
    //相机权限
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus ==AVAuthorizationStatusRestricted ||//此应用程序没有被授权访问的照片数据。可能是家长控制权限
        authStatus ==AVAuthorizationStatusDenied)  //用户已经明确否认了这一照片数据的应用程序访问
    {
        UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"提示:"
                                                           message:@"尚未开启相机权限,您可以前往设置->隐私->相册授权应用访问相机权限"
                                                          delegate:self
                                                 cancelButtonTitle:@"取消"
                                                 otherButtonTitles:@"确定", nil];
        [alterView show];
        statusblock(NO);
    }else if (authStatus == AVAuthorizationStatusNotDetermined){
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            
            if(granted) {
                statusblock(YES);

            }
            
            else{
                statusblock(NO);

            }
            
        }];
    }else{
        statusblock(YES);
    }
}
/**
 *  判断是否有相册权限
 *
 *  @return
 */
- (BOOL)judgePhotoStatus
{
    //相册权限
    ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
    if (author ==kCLAuthorizationStatusRestricted || author ==kCLAuthorizationStatusDenied){
        UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"提示:"
                                                           message:@"尚未开启相机权限,您可以前往设置->隐私->相册授权应用访问相册权限"
                                                          delegate:self
                                                 cancelButtonTitle:@"取消"
                                                 otherButtonTitles:@"确定", nil];
        [alterView show];
        return NO;
        
    }
    return YES;
}

- (BOOL)judgeLocationStatus{
    CLAuthorizationStatus author = [CLLocationManager authorizationStatus];
    if (author ==kCLAuthorizationStatusRestricted || author ==kCLAuthorizationStatusDenied){
        UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"提示:"
                                                           message:@"尚未开启定位权限,您可以前往设置->隐私->定位授权应用访问位置权限"
                                                          delegate:self
                                                 cancelButtonTitle:@"取消"
                                                 otherButtonTitles:@"确定", nil];
        [alterView show];
        return NO;

    }
    return YES;
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        // 无权限 引导去开启
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if ([[UIApplication sharedApplication]canOpenURL:url]) {
            [[UIApplication sharedApplication]openURL:url];
        }
    }
}

@end
